define(function () {
    'use strict';
    function ctrl($scope, $timeout, $state, $ionicLoading, LocalCacheService, CommonService, WTService,WebService, FinancingService) {
        var seconds = 180;
        var par = $scope.param = {};

        function upadateSeconds() {
            if (seconds == 0) {
                $scope.buttonState.checkState = false;
                $scope.buttonState.checkText = '获取验证码';
                seconds = 180;
                return;
            }
            seconds--;
            $scope.buttonState.checkText = seconds + '秒后失效';
            $timeout(function () {
                upadateSeconds();
            }, 1000);
        }

        function init() {
            var user = LocalCacheService.getUser();
            //渠道统计相关
            par.source = CommonService.getSource();

            $scope.financing = {
                sh_account: '',//沪A账户号
                sz_account: '',//深A账户号
                mobile: user.mobile_tel,
                vCode: '',
                email: ''
            };
            $scope.buttonState = {
                checkState: false,
                submitState: false,
                checkText: '获取验证码'
            };

            var sparams = {client_id: user.client_id};
            WebService.qryStockholder(sparams).then(function (data) {
                if ((data.H_A.open_status == '1' || data.H_A.open_date != null) && data.H_A.regflag == '1') {//已开通
                    $scope.financing.sh_account =data.H_A.stock_account;
                }
                if (data.S_A.open_status == '1' || data.S_A.open_date != null) {//已开通
                    $scope.financing.sz_account =data.S_A.stock_account;
                }
            });

            $scope.formEvents = {
                getCode: function () {
                    $ionicLoading.show();
                    //获取验证码
                    //查询是否有手机号的渠道信息
                    var mobile_tel = user.mobile_tel;
                    par.recommendInfos = LocalCacheService.getRecommendInfos();
                    WTService.sendMobileCode({
                        mobile_tel: mobile_tel,
                        recommendInfos: par.recommendInfos,
                        source: par.source,
                        type:'1'
                    }).then(
                        function(data){
                            $ionicLoading.hide();
                            $scope.buttonState.checkState = true;
                            upadateSeconds();
                        },
                        function(result){
                            CommonService.showAlert({message: result.error_info});
                            $ionicLoading.hide();
                        }
                    );
                },
                next: function () {
                    //表单验证通过后跳转风险评测
                    /*  $ionicLoading.show();
                    //验证手机验证码
                   WTService.checkMobileCode({
                        mobile_tel: user.mobile_tel,
                        mobile_code: $scope.financing.vCode
                    }).then(
                        function () {
                            FinancingService.setUserEmail({
                                client_id: user.client_id,
                                fund_account: user.fund_account,
                                e_mail: $scope.financing.email
                            }).then(
                                function () {
                                    $state.go('tab.account-easyFinancingRisk');
                                },
                                function(result){
                                    CommonService.showAlert({message: result.error_info});
                                    $ionicLoading.hide();
                                }
                            );
                        },
                        function (result) {
                            CommonService.showAlert({message: result.error_info});
                            $ionicLoading.hide();
                        }
                    );*/
                    $state.go('tab.account-easyFinancingRisk');
                }
            }
        }

        $scope.$on('$ionicView.beforeEnter', function () {
            init();
        });
    }

    ctrl.$inject = ['$scope', '$timeout', '$state', '$ionicLoading','LocalCacheService', 'CommonService', 'WTService','WebService', 'FinancingService'];
    return ctrl;
});